class Place
    attr_reader :size, :transports
    
    def initialize
      @size = 2
      @transports = []
    end
  
    def add(transport)
      if transport.class.name == 'Car'
        @size -= 2
      else 
        @size -= 1
      end
      @transports.push(transport)
    end
  end
  
  class Car 
    attr_reader :number
    
    def initialize(number)
      @number = number
    end
  
    def to_s
      "number = #{number}"
    end
  end
  
  class Bike < Car
  end
  
  class Parking 
    attr_reader :places
    
    def initialize
      @places = (1..20).inject({}) do |acc, index|
        acc.merge({index => Place.new})
      end
    end
  
    def add(transport)   
      index, free_place = @places.find do |index, place|
        if transport.class.name == 'Car'
          place.size == 2
        else 
          place.size >= 1
        end
      end
    
      if free_place.nil?
        puts "We dont have place for your transport, sorry :("
      else
        free_place.add(transport)
        @places[index] = free_place
      end
    end
  
    def find_transport(number)
      @places.find do |index, place|
        transport = place.transports.find do |transport|
          transport.number == number
        end
        return [index, transport]
      end
    end
  end
  
  parking = Parking.new
  
  car = Car.new "123345"
  bike = Bike.new "543321"
  
  parking.add car
  parking.add bike
  
  index, found_transport = parking.find_transport "123345"
  
  puts "place index = #{index}, transport = #{found_transport}"